package com.alex.aop.configByXML;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

public class LoggingAspect {


    public void declaredJoinPointExpression() {}

    public void beforeMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("this method " + methodName + " begins with " + args);
    }

    public void afterMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("this method "  + methodName + " ends a");
    }
    public void afterMethod2(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();

        System.out.println("this method "  + methodName + " ends b");
    }

    public void afterReturning(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("this method "  + methodName + " returns with " + result);
    }

    public void afterThrowing(JoinPoint joinPoint, Exception throwing) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("this method "  + methodName + " throws with " + throwing);
    }

    public Object around(ProceedingJoinPoint pjd) {
        Object[] args = pjd.getArgs();
        String methodName = pjd.getSignature().getName();
        System.out.println("this method "   + " around a");
        Object result = null;
        try {
            System.out.println("before");
            result = pjd.proceed();
            System.out.println("after");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("afterThrowing");
            throw new RuntimeException(throwable);
        }
        System.out.println("afterReturning");
        return result;
    }
}
